import { useModel } from "umi";

import EditApplication from "../components/organization_application/Edit";
import CreateApplicationType from "../components/organization_application/TypeCreate";
import EditApplicationType from "../components/organization_application/TypeEdit";
import EditApplicationTypeIcon from "../components/organization_application/TypeIcon";
import EditApplicationTypeVersionDescription from "../components/organization_application/VersionDesc";
import AddDependentedDatabase from "../components/organization_application/AddDependentedDatabase";
import UpdateDatabaseVersion from "../components/organization_application/UpdateDatabaseVersion";

const useHelperOrganizationApplication = () => {
    const { setModalVisible, setModalTitle, setModalWidth, setModalComponent } = useModel("global.modal");

    const openEditApplicationModal = (record, reloadData) => {
        setModalTitle("编辑应用信息");
        setModalWidth(600);
        setModalComponent(<EditApplication record={record} reloadData={reloadData} />);
        setModalVisible(true);
    };

    const openCreateApplicationType = (application, applicationTypes, reloadData) => {
        setModalTitle(application.appName);
        setModalVisible(true);
        setModalWidth(600);
        setModalComponent(<CreateApplicationType app={application} types={applicationTypes} reloadData={reloadData} />);
    };

    const openEditApplicationType = (application, item, reloadData) => {
        setModalTitle(application.appName + ` - 编辑【${item.appType}】信息`);
        setModalWidth(600);
        setModalComponent(<EditApplicationType item={item} reloadData={reloadData} />);
        setModalVisible(true);
    };

    const openEditApplicationTypeIcon = (application, item, reloadData) => {
        setModalTitle(application.appName + ` - 【${item.appType}】图标`);
        setModalVisible(true);
        setModalWidth(600);
        setModalComponent(<EditApplicationTypeIcon application={application} type={item} reloadData={reloadData} />);
    };

    const openEditApplicationTypeVerionDesc = async (item, reloadData) => {
        setModalTitle("编辑应用产品信息");
        setModalWidth(600);
        setModalComponent(<EditApplicationTypeVersionDescription version={item} reloadData={reloadData} />);
        setModalVisible(true);
    };

    const openAddApplicationDatabaseModal = async ({ application, databases, addDatabase, appDatabases, reloadData }) => {
        let newData = databases.filter((item) => appDatabases.findIndex((_item) => item.databaseName === _item.databaseName) === -1);
        setModalTitle("添加依赖的数据库");
        setModalWidth(750);
        setModalComponent(<AddDependentedDatabase application={application} databases={newData} addDatabase={addDatabase} reloadData={reloadData} />);
        setModalVisible(true);
    };

    const openUpdateApplicationDatabaseModal = async ({ application, record, index, appDatabases, addDatabase, reloadData }) => {
        setModalTitle("更改依赖的数据库模型版本");
        setModalWidth(750);
        setModalComponent(
            <UpdateDatabaseVersion application={application} index={index} database={record} appData={appDatabases} updateDatabases={addDatabase} reloadData={reloadData} />
        );
        setModalVisible(true);
    };

    return {
        openEditApplicationModal,
        openCreateApplicationType,
        openEditApplicationType,
        openEditApplicationTypeIcon,
        openEditApplicationTypeVerionDesc,
        openAddApplicationDatabaseModal,
        openUpdateApplicationDatabaseModal,
    };
};

export default useHelperOrganizationApplication;
